package cn.jekun.web.shop.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.validator.annotation.Security;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.ErpOrderMessage;
import cn.jekun.component.core.bean.gen.ErpOrderStatus;
import cn.jekun.component.core.bean.gen.ErpOrderType;
import cn.jekun.component.core.bean.gen.ErpOrders;
import cn.jekun.component.core.bean.gen.ErpPayment;
import cn.jekun.component.core.service.gen.ErpOrderStatusService;
import cn.jekun.component.core.service.gen.ErpOrderTypeService;
import cn.jekun.component.core.service.gen.ErpOrdersService;
import cn.jekun.component.core.service.gen.ErpPaymentService;
import cn.jekun.web.shop.bean.Order;
import cn.jekun.web.shop.bean.ShopMember;
import cn.jekun.web.shop.service.MemberService;
import cn.jekun.web.shop.service.OrderService;

/**
 * 订单
 * @author Mr.Dong
 *
 */
@SuppressWarnings("serial")
public class OrderAction extends ShopBaseAction{
	@Resource(name = OrderService.BEAN_NAME)
	protected OrderService orderService;
	@Resource(name = ErpPaymentService.BEAN_NAME)
	protected ErpPaymentService paymentService;
	@Resource(name = ErpOrderStatusService.BEAN_NAME)
	protected ErpOrderStatusService erpOrderStatusService;
	@Resource(name = ErpOrderTypeService.BEAN_NAME)
	protected ErpOrderTypeService erpOrderTypeService;
	@Resource(name=MemberService.BEAN_NAME)
	MemberService memberService;
	@Resource(name = cn.jekun.component.shop.service.OrderService.BEAN_NAME)
	protected cn.jekun.component.shop.service.OrderService commponentorderService;

	@Resource(name=ErpOrdersService.BEAN_NAME)
	ErpOrdersService erpOrdersService;
	
	private List<Order> myorders = new ArrayList<Order>();
	private List<ErpPayment> payments = new ArrayList<ErpPayment>();
	private List<ErpOrderStatus> orderstatus = new ArrayList<ErpOrderStatus>();
	private List<ErpOrderType> erpOrderTypes = new ArrayList<ErpOrderType>();
	private Order order;
	private List<Order> ordergoods = new ArrayList<Order>();
	private List<Order> orderlogs  = new ArrayList<Order>();
	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public List<Order> getOrdergoods() {
		return ordergoods;
	}

	public void setOrdergoods(List<Order> ordergoods) {
		this.ordergoods = ordergoods;
	}

	public List<Order> getOrderlogs() {
		return orderlogs;
	}

	public void setOrderlogs(List<Order> orderlogs) {
		this.orderlogs = orderlogs;
	}

	public List<ErpPayment> getPayments() {
		return payments;
	}

	public void setPayments(List<ErpPayment> payments) {
		this.payments = payments;
	}

	public List<ErpOrderStatus> getOrderstatus() {
		return orderstatus;
	}

	public void setOrderstatus(List<ErpOrderStatus> orderstatus) {
		this.orderstatus = orderstatus;
	}

	public List<ErpOrderType> getErpOrderTypes() {
		return erpOrderTypes;
	}

	public void setErpOrderTypes(List<ErpOrderType> erpOrderTypes) {
		this.erpOrderTypes = erpOrderTypes;
	}

	public List<Order> getMyorders() {
		return myorders;
	}

	public void setMyorders(List<Order> myorders) {
		this.myorders = myorders;
	}
	
	private ShopMember mcMember;

	public ShopMember getMcMember() {
		return mcMember;
	}
	public void setMcMember(ShopMember mcMember) {
		this.mcMember = mcMember;
	}

	private String paymentId; 
	private String beginAmt; //开始金额
	private String endAmt;   //结束金额
	private String beginDate;  //开始日期
	private String endDate;    //结束日期
	private String erpOrderType;
	private String orderstatusId;
	private String beginnums;
	private String endnums;
	private String orderId;
	private String payStatus;
	private String userName;
	
	
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPayStatus() {
		return payStatus;
	}

	public void setPayStatus(String payStatus) {
		this.payStatus = payStatus;
	}

	public String getPaymentId() {
		return paymentId;
	}

	public void setPaymentId(String paymentId) {
		this.paymentId = paymentId;
	}

	public String getBeginAmt() {
		return beginAmt;
	}

	public void setBeginAmt(String beginAmt) {
		this.beginAmt = beginAmt;
	}

	public String getEndAmt() {
		return endAmt;
	}

	public void setEndAmt(String endAmt) {
		this.endAmt = endAmt;
	}

	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getErpOrderType() {
		return erpOrderType;
	}

	public void setErpOrderType(String erpOrderType) {
		this.erpOrderType = erpOrderType;
	}

	public String getOrderstatusId() {
		return orderstatusId;
	}

	public void setOrderstatusId(String orderstatusId) {
		this.orderstatusId = orderstatusId;
	}

	public String getBeginnums() {
		return beginnums;
	}

	public void setBeginnums(String beginnums) {
		this.beginnums = beginnums;
	}

	public String getEndnums() {
		return endnums;
	}

	public void setEndnums(String endnums) {
		this.endnums = endnums;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	/**
	 * 订单详情
	 * @param map
	 * @return
	 */
	public String getOrderDetails(){
		JSONObject jsonObject=new JSONObject();

		try {
			
			this.order = this.orderService.getOrder(request.getParameter("orderId"));
			
			jsonObject.put("paymentId", order.getPaymentId()==null?"":order.getPaymentId());
			jsonObject.put("statusId", order.getStatusId()==null?"":order.getStatusId());
			jsonObject.put("orderId", order.getOrderId()==null?"":order.getOrderId());
			jsonObject.put("statusName", order.getStatusName()==null?"":order.getStatusName());
			jsonObject.put("orderTypeId", order.getOrderTypeId()==null?"":order.getOrderTypeId());
			jsonObject.put("orderTypeName", order.getOrderTypeName()==null?"":order.getOrderTypeName());
			jsonObject.put("orderDate", order.getOrderDate()==null?"":DateToDString(order.getOrderDate()));
			jsonObject.put("payTime",  order.getPayTime()==null?"":DateToDString(order.getPayTime()));
			jsonObject.put("finishTime", order.getFinishTime()==null?"":DateToDString(order.getFinishTime()));
			jsonObject.put("payMentName", order.getPayMentName() ==null?"":order.getPayMentName());
			jsonObject.put("deliveryId", order.getDeliveryId() ==null?"":order.getDeliveryId());
			jsonObject.put("deliveryName", order.getDeliveryName() ==null?"":order.getDeliveryName());
			jsonObject.put("shipName", order.getShipName() ==null?"":order.getShipName());
			jsonObject.put("shipMobile", order.getShipMobile() ==null?"":order.getShipMobile());
			jsonObject.put("shipTime", order.getShipTime() ==null?"":order.getShipTime());
			jsonObject.put("shipAddress", order.getShipAddress() ==null?"":order.getShipAddress());
			jsonObject.put("memo", order.getMemo() ==null?"":order.getMemo());
			Double totalAmount = order.getTotalAmount()==null?0:order.getTotalAmount();
			Double deliveryAmount = order.getDeliveryAmount()==null?0:order.getDeliveryAmount();
			jsonObject.put("totalAmount", totalAmount);
			jsonObject.put("deliveryAmount", deliveryAmount);
			jsonObject.put("merchandiseAmountMoney", totalAmount - deliveryAmount);//商品价格总计 = 订单总金额 - 配送费
			jsonObject.put("shopName", order.getShopName()==null?"":order.getShopName());
			jsonObject.put("businessId", order.getBusinessId()==null?"":order.getBusinessId());
			jsonObject.put("businessName", order.getBusinessName()==null?"":order.getBusinessName());
			jsonObject.put("point", order.getPoint()==null?"":order.getPoint());
			jsonObject.put("batchNo", order.getBatchNo()==null?"":order.getBatchNo());
			jsonObject.put("vouchNo", order.getVouchNo()==null?"":order.getVouchNo());
			jsonObject.put("closeReason", order.getCloseReason()==null?"":order.getCloseReason());
			jsonObject.put("rejectReceiptTime", order.getRejectReceiptTime()==null?"":DateToDString(order.getRejectReceiptTime()));
			jsonObject.put("rejectReceiptReason", order.getRejectReceiptReason()==null?"":order.getRejectReceiptReason());
			jsonObject.put("userMediateTime", order.getUserMediateTime()==null?"":DateToDString(order.getUserMediateTime()));
			jsonObject.put("userMediateReason", order.getUserMediateReason()==null?"":order.getUserMediateReason());
			jsonObject.put("shopMediateTime", order.getShopMediateTime()==null?"":DateToDString(order.getShopMediateTime()));
			jsonObject.put("shopMediateReason", order.getShopMediateReason()==null?"":order.getShopMediateReason());
			jsonObject.put("sn", order.getSn() == null?"":order.getSn());
			
			//超时消息
			ErpOrders tempOrder = this.erpOrdersService.one(orderId);
			jsonObject.put("timeoutMessage", this.commponentorderService.getTimeoutMessage(tempOrder, this.commponentorderService.TYPE_SHOP));
			
			ErpOrders tipOrder=new ErpOrders();
			tipOrder.setOrderId(order.getOrderId());
			tipOrder.setOrderStatusId(order.getStatusId());
			tipOrder.setBusinessId(order.getBusinessId());
			tipOrder.setPaymentId(order.getPaymentId());
			tipOrder.setDeliverTime(order.getDeliverTime());
			tipOrder.setRejectReceiptTime(order.getRejectReceiptTime());
			tipOrder.setOrderTypeId(order.getOrderTypeId());
			
			
			//是否出现取消订单
			boolean flagCancelOrder=this.commponentorderService.allowCancelOrder(tipOrder,cn.jekun.component.shop.service.OrderService.TYPE_SHOP);
			if(flagCancelOrder){
				jsonObject.put("allowCancelOrder", "1");
			}else{
				jsonObject.put("allowCancelOrder", "0");
			}
			
			boolean flagShippingOrder=this.commponentorderService.allowShippingOrder(tipOrder, cn.jekun.component.shop.service.OrderService.TYPE_SHOP);
			if(flagShippingOrder){
				jsonObject.put("allowShippingOrder", "1");
			}else{
				jsonObject.put("allowShippingOrder", "0");
			}
			
			//是否出现提醒用户收货
			boolean flagRemindToUser=this.commponentorderService.allowRemind(tipOrder,cn.jekun.component.shop.service.OrderService.REMIND_TO_USER);
			if(flagRemindToUser){
				jsonObject.put("allowRemindToUser", "1");
			}else{
				jsonObject.put("allowRemindToUser", "0");
			}
			
			//是否出现客服介入
			boolean flagApply=this.commponentorderService.allowApply(tipOrder);
			if(flagApply){
				jsonObject.put("allowApply", "1");
			}else{
				jsonObject.put("allowApply", "0");
			}
			
			boolean flagFinish=this.commponentorderService.allowFinishOrder(tipOrder, cn.jekun.component.shop.service.OrderService.TYPE_SHOP);
			if(flagFinish){
				jsonObject.put("allowFinish", "1");
			}else{
				jsonObject.put("allowFinish", "0");
			}
			
			JSONArray jsonArray=new JSONArray();
			
			HashMap<String, Object> hashMap;
			
			this.ordergoods = this.orderService.getOrderGoods(request.getParameter("orderId"));
			
			double totalMoney=0;
			
			for(int i=0;i<ordergoods.size();i++){
				
				hashMap=new HashMap<String, Object>();
				
				hashMap.put("picDefault", ordergoods.get(i).getPicDefault()==null?"":ordergoods.get(i).getPicDefault());
				
				hashMap.put("merchandiseName", ordergoods.get(i).getMerchandiseName()==null?"":ordergoods.get(i).getMerchandiseName());
				
				hashMap.put("barcode", ordergoods.get(i).getBarcode()==null?"":ordergoods.get(i).getBarcode());
				
				hashMap.put("price", ordergoods.get(i).getPrice()==null?"":ordergoods.get(i).getPrice());
				
				hashMap.put("nums", ordergoods.get(i).getNums()==null?"":ordergoods.get(i).getNums());
				
				hashMap.put("amount", ordergoods.get(i).getAmount()==null?"":ordergoods.get(i).getAmount());
				
				totalMoney = totalMoney+ordergoods.get(i).getAmount();
				
				jsonArray.add(hashMap);
				
			}
			
			jsonObject.put("ordergoods", jsonArray);
			
			//订单留言
			List<ErpOrderMessage>  erpOrderMessagesList= this.orderService.getOrderMessage(request.getParameter("orderId"));
			
			jsonArray=new JSONArray();
			
			for(int i=0;i<erpOrderMessagesList.size();i++){
				
				hashMap=new HashMap<String, Object>();
				
				hashMap.put("createTime",erpOrderMessagesList.get(i).getCreateTime()==null?"":DateToDString(erpOrderMessagesList.get(i).getCreateTime()));
				
				hashMap.put("content", erpOrderMessagesList.get(i).getContent()==null?"":erpOrderMessagesList.get(i).getContent());
				
				hashMap.put("attach", erpOrderMessagesList.get(i).getAttach()==null?"":erpOrderMessagesList.get(i).getAttach());
				
				hashMap.put("createName", erpOrderMessagesList.get(i).getCreateName()==null?"":erpOrderMessagesList.get(i).getCreateName());
				
				jsonArray.add(hashMap);
				
			}
			
			jsonObject.put("orderMessage", jsonArray);

			
			this.orderlogs  = this.orderService.getOrderLog(request.getParameter("orderId"));
			
			jsonArray=new JSONArray();
			
			
			for(int i=0;i<orderlogs.size();i++){
				
				hashMap=new HashMap<String, Object>();
				
				hashMap.put("createTime",orderlogs.get(i).getCreateTime()==null?"":DateToDString(orderlogs.get(i).getCreateTime()));
				
				hashMap.put("memo", orderlogs.get(i).getMemo()==null?"":orderlogs.get(i).getMemo());
				
				hashMap.put("operator", orderlogs.get(i).getOperator()==null?"":orderlogs.get(i).getOperator());
				
				jsonArray.add(hashMap);
				
			}
			
			jsonObject.put("orderlogs", jsonArray);
			
			System.out.println(totalMoney);
			jsonObject.put("totalMoney",totalMoney);
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
		
		
		
		
	}



	/**
	 * 订单列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getMyOrders(){
		
		JSONObject jsonObject=new JSONObject();
		try {
//			formMap.put("beginDate", beginDate);
//			formMap.put("endDate", endDate);
//			formMap.put("beginAmt", beginAmt);
//			formMap.put("endAmt",endAmt);
//			formMap.put("paymentId",paymentId);
//			formMap.put("erpOrderType",erpOrderType);
//			formMap.put("beginnums",beginnums);
//			formMap.put("endnums",endnums);
//			formMap.put("orderstatusId",orderstatusId);
//			formMap.put("orderId",orderId);
//			formMap.put("shopId",this.getSessionUser().getShopId());
//			formMap.put("payStatus",payStatus);
			
			String beginDate=request.getParameter("beginDate");
			String endDate=request.getParameter("endDate");
			String beginAmt=request.getParameter("beginAmt");
			String endAmt=request.getParameter("endAmt");
			String paymentId=request.getParameter("paymentId");
			String erpOrderType=request.getParameter("erpOrderType");
			String beginnums=request.getParameter("beginnums");
			String endnums=request.getParameter("endnums");
			String orderstatusId=request.getParameter("orderstatusId");
			String orderId=request.getParameter("orderId");
			String shopId=this.getSessionUser().getShopId();
			String payStatus=request.getParameter("payStatus");
			
			String userId=null;
			
			request.getSession().removeAttribute("flag");
			if(StringUtils.isNotBlank(request.getParameter("userId"))){
				userId=request.getParameter("userId");
				request.getSession().setAttribute("flag", request.getParameter("userId"));
				this.mcMember = this.memberService.getMemberDetails(request.getParameter("userId")
						,this.getSessionUser().getShopId());
			}
			this.myorders = this.orderService.getMyOrders(beginDate,endDate,beginAmt,endAmt,paymentId,erpOrderType,beginnums,endnums,
					orderstatusId,orderId,shopId,payStatus,userId,userName,page);



			JSONArray jsonArray=new JSONArray();

			HashMap<String, Object> hashMap;
			
			if(this.myorders.size()>0){
				for(int i=0;i<this.myorders.size();i++){

					hashMap=new HashMap<String, Object>();
					hashMap.put("OrderId", myorders.get(i).getOrderId());
					hashMap.put("OrderTypeName", myorders.get(i).getOrderTypeName()==null?"":myorders.get(i).getOrderTypeName());
					hashMap.put("BusinessName", myorders.get(i).getBusinessName()==null?"":myorders.get(i).getBusinessName());
					hashMap.put("PayMentName", myorders.get(i).getPayMentName()==null?"":myorders.get(i).getPayMentName());
					hashMap.put("deliveryName", myorders.get(i).getDeliveryName()==null?"":myorders.get(i).getDeliveryName());
					hashMap.put("OrderStatusName", myorders.get(i).getOrderStatusName()==null?"":myorders.get(i).getOrderStatusName());
					hashMap.put("TotalAmount", myorders.get(i).getTotalAmount()==null?"":myorders.get(i).getTotalAmount());
					hashMap.put("Nums", myorders.get(i).getNums()==null?"":myorders.get(i).getNums());
					hashMap.put("Point", myorders.get(i).getPoint()==null?"":myorders.get(i).getPoint());
					hashMap.put("OrderTime", myorders.get(i).getOrderTime()==null?"":DateToDString(myorders.get(i).getOrderTime()));
					hashMap.put("UserName", myorders.get(i).getUserName()==null?"":myorders.get(i).getUserName());

					jsonArray.add(hashMap);

					jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

					jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");

					jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
					
					jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
					
					jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
					
					jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());
								
					jsonObject.put("orderListData", jsonArray);

				}
			}else{
				jsonObject.put(ErrorMsg.KEY_CODE, 2);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"没有查询到相关数据！");
				
				jsonObject.put("orderListData", jsonArray);
			}

			
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}

	
	/**
	 * 查询支付方式列表
	 * @return
	 */
	public String getPaymentsList(){
		
		JSONObject jsonObject=new JSONObject();

		this.payments = this.paymentService.list(null);
		
		JSONArray jsonArray=new JSONArray();
		
		HashMap<String, Object> hashMap;
		
		for(int i=0;i<this.payments.size();i++){
			
			hashMap=new HashMap<String, Object>();
			
			ErpPayment tempErpPayment=payments.get(i);
			
			hashMap.put(ErpPayment.COL_PAYMENT_ID, tempErpPayment.getPaymentId());
			
			hashMap.put(ErpPayment.COL_PAYMENT_NAME, tempErpPayment.getPaymentName());
			
			jsonArray.add(hashMap);
			
		}
		
		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
		
		jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		
		jsonObject.put("paymentsListData", jsonArray);
		
		return this.writeJson(jsonObject);
	}
	
	
	/**
	 * 查询订单类型列表
	 * @return
	 */
	public String getErpOrderTypesList(){
		
		JSONObject jsonObject=new JSONObject();

		this.erpOrderTypes = this.erpOrderTypeService.list(null);
		
		JSONArray jsonArray=new JSONArray();
		
		HashMap<String, Object> hashMap;
		
		for(int i=0;i<this.erpOrderTypes.size();i++){
			
			hashMap=new HashMap<String, Object>();
			
			hashMap.put(ErpOrderType.COL_ORDER_TYPE_ID, erpOrderTypes.get(i).getOrderTypeId());
			
			hashMap.put(ErpOrderType.COL_ORDER_TYPE_NAME, erpOrderTypes.get(i).getOrderTypeName());
			
			jsonArray.add(hashMap);
			
		}
		
		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
		
		jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		
		jsonObject.put("orderTypesListData", jsonArray);
		
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 查询订单状态列表
	 * @return
	 */
	public String getOrderstatusList(){
		
		JSONObject jsonObject=new JSONObject();
	
		this.orderstatus = this.erpOrderStatusService.list(null);
		
		JSONArray jsonArray=new JSONArray();
		
		HashMap<String, Object> hashMap;
		
		for(int i=0;i<this.orderstatus.size();i++){
			
			hashMap=new HashMap<String, Object>();
			
			hashMap.put(ErpOrderStatus.COL_ORDER_STATUS_ID, orderstatus.get(i).getOrderStatusId());
			
			hashMap.put(ErpOrderStatus.COL_ORDER_STATUS_NAME, orderstatus.get(i).getOrderStatusName());
			
			jsonArray.add(hashMap);
			
		}
		
		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
		
		jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		
		jsonObject.put("orderStatusListData", jsonArray);
		
		return this.writeJson(jsonObject);
	}
	
	

	/**
	 * 商家处理订单(app,订单处理)
	 * @return
	 */
	public String processOrder(){

//		try{
//			if(Constants.ERP_ORDER_STATUS.SHIPPING.equals(request.getParameter("type"))){//发货
//				this.commponentorderService.shippingOrder(this.getSessionUser().getUserId()
//						, request.getParameter("orderId"),null);
//				this.logService.addShopAuditLog(null, "商家处理订单(发货)成功，订单代码，"+request.getParameter("orderId"));
//			}else if(Constants.ERP_ORDER_STATUS.CLOSED.equals(request.getParameter("type"))){//取消
//				this.commponentorderService.cancelOrderByShop(this.getSessionUser().getUserId()
//						, request.getParameter("orderId"),null);
//				this.logService.addShopAuditLog(null, "商家处理订单(取消/关闭)成功，订单代码，"+request.getParameter("orderId"));
//			}else if(Constants.ERP_ORDER_STATUS.OK.equals(request.getParameter("type"))){//完成
//				this.commponentorderService.finishOrder(this.getSessionUser().getUserId()
//						, request.getParameter("orderId"),null);
//				this.logService.addShopAuditLog(null, "商家处理订单(完成)成功，订单代码，"+request.getParameter("orderId"));
//			}
//		}catch (BusinessException e) {
//			this.saveActionError(e.getErrorMsg().getMessage());
//		}catch (Exception e) {
//			e.printStackTrace();
//			//this.saveActionError("操作失败");
//		}
		//return "/shop/Order/getOrderDetails.do";
		
		JSONObject jsonObject=new JSONObject();
		try{
			if(Constants.ERP_ORDER_STATUS.CLOSED.equals(request.getParameter("type"))){//取消
				String memo = request.getParameter("memo");//备注
				String reason = request.getParameter("reason");//原因编码
				this.commponentorderService.cancelOrderByShop(this.getSessionUser().getUserId(), request.getParameter("orderId"),memo,reason);
			
			}else if(Constants.ERP_ORDER_STATUS.SHIPPING.equals(request.getParameter("type"))){//发货
				
				this.commponentorderService.shippingOrderByShop(this.getSessionUser().getUserId()
						, request.getParameter("orderId"),null);
				
				
			}else if(Constants.ERP_ORDER_STATUS.OK.equals(request.getParameter("type"))){
				
				this.commponentorderService.finishOrderByShop(this.getSessionUser().getUserId()
						, request.getParameter("orderId"),null);
			}else if(Constants.ERP_ORDER_STATUS.REJECT_RECEIPT.equals(request.getParameter("type"))){//在拒绝收货中，提出客服介入
				String memo = request.getParameter("memo");//备注
				String reason = request.getParameter("reason");//原因编码
				String orderId=request.getParameter("orderId");//订单编码
				String fileUrl=request.getParameter("fileUrl");//附件路径
		    	String processCode="4";//客服介入代码
		    	String processer="2";//商家操作代码
//				this.commponentorderService.cancelOrderByShop(this.getSessionUser().getUserId(), request.getParameter("orderId"),memo,reason);
				this.commponentorderService.processOrder(this.getSessionUser().getUserId()
						, orderId, processCode, processer, reason, fileUrl, memo,null);
			}
			
			this.logService.addAdminAuditLog(null, "商家处理订单成功，订单号，"+request.getParameter("orderId"));
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"处理订单成功");
			
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		
		return this.writeJson(jsonObject);
	}


//	/**
//	 * 打印订单(推送)
//	 * @return
//	 */
//	public String printOrder(){
//		JSONObject jsonObject=new JSONObject();
//		try{
//			String orderId=request.getParameter("orderId");
//			this.orderService.pushOrderPrintByOrderId(orderId);
//			
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
//			
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"打印订单成功");
//			
//			
//			
//		}catch (BusinessException e) {
//			jsonObject = JSONObject.fromObject(e.getErrorMsg());
//		} catch (Exception e) {
//			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
//			e.printStackTrace();
//		}
//		return this.writeJson(jsonObject);
//	}

	/**
     * 获取取消原因
     * @return
     */
    public String getReasonList(){
    	
    	String processCode=request.getParameter("processCode");
    	String orderId=request.getParameter("orderId");
    	
    	JSONObject jsonObject;
		try {
			if(processCode.equals("取消")){
				processCode=Constants.ERP_ORDER_PROCESS_BY_SHOP.GO_TO_CANCEL;
			}else if(processCode.equals("申请客服介入")){
				processCode=Constants.ERP_ORDER_PROCESS_BY_SHOP.GO_TO_APPLY;
			}
			jsonObject = this.commponentorderService.reasonList(this.getSessionUser().getUserId(),processCode, cn.jekun.component.shop.service.OrderService.TYPE_SHOP , orderId);
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
    
    	return this.writeJson(jsonObject);
    }
    
    /**
     * 提醒用户收货
     * @return
     */
    public String remindToUser(){
    	
    	String orderId=request.getParameter("orderId");
    	String processCode="3";//提醒用户收货代码
    	String processer="2";//商家操作代码
    	JSONObject jsonObject;
    	
    	try {
			jsonObject = this.commponentorderService.processOrder(this.getSessionUser().getUserId(), orderId, processCode, processer, null, null, null,null);
   
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
    
    	return this.writeJson(jsonObject);
    
    }
    /**
     * 获取新增订单
     * @return
     */
    public String checkOrderAdd(){
    	if (this.getSessionUser() != null && this.getSessionUser().getShopId() !=null) {
    		String checkTime = request.getParameter("checkTime");
    		
    		JSONObject jsonObject = this.orderService.checkOrderAdd(checkTime,this.getSessionUser().getShopId());
    		
    		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
    		
    		jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
    		return this.writeJson(jsonObject);
		}else{
			JSONObject jsonObject = new JSONObject();
    		
    		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
    		
    		jsonObject.put(ErrorMsg.KEY_MESSAGE,"没有登录");
    		return this.writeJson(jsonObject);
		}
    
    }
    /**
     * 设置打印类型
     * @return
     */
    public String setPrintType(){
    	if (this.getSessionUser() != null && this.getSessionUser().getShopId() !=null) {
    		String printType = request.getParameter("printType");
    		
    		JSONObject jsonObject = this.orderService.setPrintType(printType,this.getSessionUser().getShopId());
    		
    		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
    		
    		jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
    		
    		this.getSessionUser().setShopPrintType(printType);
    		
    		return this.writeJson(jsonObject);
		}else{
			JSONObject jsonObject = new JSONObject();
    		
    		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
    		
    		jsonObject.put(ErrorMsg.KEY_MESSAGE,"没有登录");
    		return this.writeJson(jsonObject);
		}
    
    }
    
}
